import { defineConfig, loadEnv } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
// import visualizer from 'rollup-plugin-visualizer'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    const VITE_BASE_URL: string = loadEnv(mode, process.cwd()).VITE_BASE_URL
    return {
        base: VITE_BASE_URL, // 添加这个属性
        plugins: [
            vue({
                // refTransform: true // 开启ref转换
            }),
            // visualizer({
            //   // 打包后自动打开分析报告
            //   open: true
            // }),
            AutoImport({
                resolvers: [
                    ElementPlusResolver({
                        importStyle: 'sass'
                    })
                ]
            }),
            Components({
                dts: true,
                types: [
                    {
                        from: 'vue-router',
                        names: ['RouterLink', 'RouterView']
                    }
                ],
                resolvers: [
                    ElementPlusResolver({
                        importStyle: 'sass'
                    })
                ],
                include: [/\.vue$/, /\.vue\?vue/, /\.md$/]
            }),
            VueSetupExtend()
        ],
        server: {
            port: 4000,
            proxy: {
                '/sso': {
                    target: 'http://127.0.0.1:9100',
                    // target: 'http://192.168.230.94:9100',
                    changeOrigin: true //是否跨域
                    // 拦截到的请求路径 /api开头的uri，/testaxios会被替换成空
                    // rewrite: (path) => path.replace(/^\/sso/, '')
                },
                '/research': {
                    target: 'http://127.0.0.1:9100',
                    // target: 'http://123.103.118.57/api/',
                    // target: 'http://192.168.230.94:9100',
                    changeOrigin: true //是否跨域
                    // 拦截到的请求路径 /api开头的uri，/testaxios会被替换成空
                    // rewrite: (path) => path.replace(/^\/research/, '')
                },
                '/job': {
                    target: 'http://127.0.0.1:9100',
                    // target: 'http://123.103.118.57/api/',
                    // target: 'http://192.168.230.94:9100',
                    changeOrigin: true //是否跨域
                    // 拦截到的请求路径 /api开头的uri，/testaxios会被替换成空
                    // rewrite: (path) => path.replace(/^\/job/, '')
                }
            }
        },
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src')
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `@use "@/assets/styles/element-theme.scss" as *;`
                }
            },
            postcss: {
                plugins: [
                    require('autoprefixer'),
                    require('tailwindcss'),
                    require('postcss-import')
                ]
            }
        },
        // 分割打包
        build: {
            sourcemap: false, // 开启 CSS source maps
            // Turning off brotliSize display can slightly reduce packaging time
            brotliSize: false,
            chunkSizeWarningLimit: 2000,
            // rollupOptions: {
            //   output: {
            //     // 分割打包
            //     manualChunks(id) {
            //       if (id.includes('node_modules')) {
            //         const path = id.toString().split('node_modules/')[2]
            //         const name = path.split('/')[0].toString()
            //         return name
            //       }
            //     }
            //   }
            // },
            commonjsOptions: {
                requireReturnsDefault: 'namespace' // 要求ES模块返回其名称空间，但插件不添加标记，从而创建更高效的代码
            }
        }
    }
})
